package com.ruoyi.workbench.utils;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.workbench.domain.WbLeafUnit;
import com.ruoyi.workbench.domain.WbVisitLog;
import com.ruoyi.workbench.domain.WbWholeUnit;
import com.ruoyi.workbench.domain.WbWholeVisitLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * 生成器
 *
 * @author hanjiafeng
 * @date 2021年2月26日 11点28分
 */
public class Generator {

    private static final Logger LOGGER = LoggerFactory.getLogger(Generator.class);

    public static SysDept initDept(String deptId) {
        SysDept dept = null;
        try {
            Long id = Long.valueOf(deptId);
            dept = new SysDept();
            dept.setDeptId(id);
        } catch (NumberFormatException e) {
            LOGGER.info("无单元id");
        }
        return dept;
    }

    public static WbWholeVisitLog generateWholeVisitLog(WbVisitLog wbVisitLog, HttpServletRequest httpServletRequest) {
        WbWholeUnit wholeUnit = generateWholeUnit(httpServletRequest);
        String unitId7 = httpServletRequest.getParameter("unitId7");
        WbLeafUnit unit7 = null;
        try {
            Long id = Long.valueOf(unitId7);
            unit7 = new WbLeafUnit();
            unit7.setUnitId(id);
        } catch (NumberFormatException e) {
            LOGGER.info("无七级单元id");
        }
        wholeUnit.setUnit7(unit7);
        WbWholeVisitLog wbWholeVisitLog = new WbWholeVisitLog();
        wbWholeVisitLog.setWholeUnit(wholeUnit);
        wbWholeVisitLog.setWbVisitLog(wbVisitLog);

        return wbWholeVisitLog;
    }

    public static WbWholeUnit generateWholeUnit(HttpServletRequest httpServletRequest) {
        String unitId3 = httpServletRequest.getParameter("unitId3");
        SysDept unit3 = initDept(unitId3);
        String unitId4 = httpServletRequest.getParameter("unitId4");
        SysDept unit4 = initDept(unitId4);
        String unitId5 = httpServletRequest.getParameter("unitId5");
        SysDept unit5 = initDept(unitId5);
        String unitId6 = httpServletRequest.getParameter("unitId6");
        SysDept unit6 = initDept(unitId6);
        WbWholeUnit wholeUnit = new WbWholeUnit();
        wholeUnit.setUnit3(unit3);
        wholeUnit.setUnit4(unit4);
        wholeUnit.setUnit5(unit5);
        wholeUnit.setUnit6(unit6);
        return wholeUnit;
    }

    /**
     * 查找str中,的数量
     *
     * @param str str
     * @return 数量
     */
    public static int getCommonCount(String str) {
        int start = str.startsWith(",") ? 1 : 0;
        int end = str.endsWith(",") ? 1 : 0;
        String[] tempArray = str.split(",");
        return tempArray.length - 1 + start + end;
    }

    public static Integer parseInteger(HttpServletRequest httpServletRequest, String str) {
        Integer integer = null;
        try {
            integer = Integer.parseInt(httpServletRequest.getParameter(str));
        } catch (NumberFormatException e) {
            LOGGER.warn(str + "不存在");
        }
        return integer;
    }

    /**
     * 初始化日期
     *
     * @param httpServletRequest httpServletRequest
     * @return 日期
     */
    public static DateUtil initDateUtil(HttpServletRequest httpServletRequest) {
        Integer period = Generator.parseInteger(httpServletRequest, "period");
        period = period == -1 ? null : period;
        String startTimeStr = null;
        String endTimeStr = null;
        if (Objects.isNull(period)) {
            startTimeStr = httpServletRequest.getParameter("startTime");
            endTimeStr = httpServletRequest.getParameter("endTime");
        } else if (period == 1) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            Date yesterday = calendar.getTime();
            String timeStr = simpleDateFormat.format(yesterday);
            startTimeStr = timeStr;
            endTimeStr = timeStr;
        }
        return new DateUtil(period, startTimeStr, endTimeStr);
    }
}
